package zdc.display {
import adobe.utils.CustomActions;

import flash.display.Bitmap;
import flash.display.BitmapData;

import starling.core.Starling;
import starling.display.Image;
import starling.events.EnterFrameEvent;
import starling.textures.Texture;
import starling.display.Sprite;

import zdc.interfaces.ISeqDisplayObject;
import zdc.Util;

/**
 * 序列帧图片
 * @author Metal Slime
 */
public class SeqImage extends Image implements ISeqDisplayObject {
    private var _textures:Array = [];
    private var _curFrame:uint = 1;
    private var _totalFrame:uint = 0;
    private var _starling:Starling;
    private var _playing:Boolean = false;

    /**
     * 对象数组，可以是字符串，bitmap对象，bitmapdata对象，Texture对象，或者bitmapdata的类
     * @param    bitmaps
     */
    public function SeqImage(bitmaps:Object) {
        trace("OK");
        _starling = Starling.current;
        asignTextures(bitmaps);
        _totalFrame = _textures.length;
        super(getTexture(0));
    }

    /**
     * 对象数组，可以是字符串，bitmap对象，bitmapdata对象，Texture对象，或者bitmapdata的类
     * @param    bitmaps
     * @param    disposeLast
     */
    public function setTextures(bitmaps:Object, disposeLast:Boolean = true):void {
        if (_textures && disposeLast) {
            for each (var t:Texture in _textures) {
                t.dispose();
            }
        }

        asignTextures(bitmaps);
        _totalFrame = _textures.length;
        _curFrame = _curFrame > _totalFrame ? 1 : _curFrame;
        this.texture = getTexture(_curFrame - 1);
    }

    /**
     * 对象数组，可以是字符串，bitmap对象，bitmapdata对象，Texture对象，或者bitmapdata的类
     * @param    bitmaps
     */
    private function asignTextures(bitmaps:Object):void {
        if (!(bitmaps is Array || bitmaps is Vector)) {
            throw new Error("参数必须为数组或者Vector！");
        }
        _textures = [];
        for (var i:int = 0; i < bitmaps.length; i++) {
            var b:Object = bitmaps[i];
            if (b is String) {
                _textures.push(Util.getClass(_starling.nativeStage.loaderInfo, b as String));
            }
            else {
                _textures.push(b);
            }
        }
    }

    /**
     * 下一帧
     */
    public function nextFrame():void {
        if (_curFrame == _totalFrame) {
            return;
        }
        _curFrame++;
        this.texture = getTexture(_curFrame - 1);
    }

    /**
     * 上一帧
     */
    public function prevFrame():void {
        if (_curFrame <= 1) {
            return;
        }
        _curFrame--;
        this.texture = getTexture(_curFrame - 1);
    }

    /**
     * 跳到某一帧
     * @param    frame
     */
    public function gotoFrame(frame:uint):void {
        if (frame > 0 && frame <= _totalFrame) {
            _curFrame = frame;
            this.texture = getTexture(_curFrame - 1);
        }
    }

    /**
     * 播放
     */
    public function play():void {
        _playing = true;
        addEventListener(EnterFrameEvent.ENTER_FRAME, onFrame);
    }

    /**
     * 停止
     */
    public function stop():void {
        _playing = false;
        _curFrame = 1;
        removeEventListener(EnterFrameEvent.ENTER_FRAME, onFrame);
    }

    /**
     * 暂停
     */
    public function pause():void {
        _playing = false;
        removeEventListener(EnterFrameEvent.ENTER_FRAME, onFrame);
    }

    private function onFrame(e:EnterFrameEvent):void {
        if (_curFrame >= _totalFrame) {
            _curFrame = 1;
        }
        else {
            _curFrame++;
        }
        this.texture = getTexture(_curFrame - 1);
    }

    private function getTexture(index:uint):Texture {
        var t:Object = _textures[index];
        if (t is Class) {
            return Texture.fromBitmapData(new t());
        }
        else if (t is BitmapData) {
            return Texture.fromBitmapData(t as BitmapData);
        }
        else if (t is Bitmap) {
            return Texture.fromBitmap(t as Bitmap);
        }
        else if (t is Texture) {
            return t as Texture;
        }
        return t as Texture;
    }

    /**
     * 总帧数
     */
    public function get totalFrame():uint {
        return _totalFrame;
    }

    /**
     * 当前帧数
     */
    public function get currentFrame():uint {
        return _curFrame;
    }

    /**
     * 是否播放中
     */
    public function get playing():Boolean {
        return _playing;
    }

    /**
     * 获取材质序列
     */
    public function get textures():Array {
        return _textures;
    }

    /**
     *    设置材质序列
     */
    public function set textures(arr:Array):void {
        setTextures(arr);
    }
}

}